package com.simple.crawler.core;

import com.simple.crawler.utils.StringUtils;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Properties;

@Component
public class SchedulerManager {

    private StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();

    /**
     * 初始化调度器
     */
    public void initialize(Properties prop) throws SchedulerException {
        if (prop != null) {
            schedulerFactory.initialize(prop);
        }
        //启动默认的执行器
        schedulerFactory.getScheduler().start();
    }

    /**
     * 根据名字获取调度器，没有名字就获取默认的调度器
     * @param schedulerName
     * @return
     * @throws SchedulerException
     */
    public Scheduler getScheduler(String schedulerName) throws SchedulerException {
        if (StringUtils.isNotBlank(schedulerName)) {
            return schedulerFactory.getScheduler(schedulerName);
        }
        return schedulerFactory.getScheduler();
    }

    /**
     * 获取所有的调度器
     * @return
     * @throws SchedulerException
     */
    public Collection<Scheduler> getAllScheduler() throws SchedulerException {
        return schedulerFactory.getAllSchedulers();
    }


    /**
     * 添加job
     * @param jobClass
     * @param scheduleBuilder
     * @throws SchedulerException
     */
    public void addJob(Class<? extends Job> jobClass, ScheduleBuilder scheduleBuilder) throws SchedulerException {
        addJob(null, jobClass, scheduleBuilder);
    }

    /**
     * 添加job
     * @param schedulerName
     * @param jobClass
     * @param scheduleBuilder
     * @throws SchedulerException
     */
    public void addJob(String schedulerName, Class<? extends Job> jobClass, ScheduleBuilder scheduleBuilder) throws SchedulerException {
        JobDetail job = JobBuilder.newJob(jobClass).
                withIdentity(jobClass.getName()).
                build();
        Trigger trigger = TriggerBuilder.newTrigger().
                forJob(job).
                withIdentity(jobClass.getName()).
                withSchedule(scheduleBuilder).
                startNow().
                build();
        getScheduler(schedulerName).scheduleJob(job, trigger);
    }

    /**
     * 删除job
     * @param jobClass
     * @throws SchedulerException
     */
    public void delJob(Class<? extends Job> jobClass) throws SchedulerException {
        delJob(null, jobClass);
    }

    /**
     * 删除job
     * @param schedulerName
     * @param jobClass
     * @throws SchedulerException
     */
    public void delJob(String schedulerName, Class<? extends Job> jobClass) throws SchedulerException {
        getScheduler(schedulerName).deleteJob(new JobKey(jobClass.getSimpleName()));
    }

    public void pause(Class<? extends Job> jobClass) throws SchedulerException {
        pause(null, jobClass);
    }

    public void pause(String schedulerName, Class<? extends Job> jobClass) throws SchedulerException {
        getScheduler(schedulerName).pauseJob(new JobKey(jobClass.getSimpleName()));
    }

    public void pauseAll(Class<? extends Job> jobClass) throws SchedulerException {
        pauseAll(null, jobClass);
    }

    public void pauseAll(String schedulerName, Class<? extends Job> jobClass) throws SchedulerException {
        getScheduler(schedulerName).pauseAll();
    }

    public void resume(Class<? extends Job> jobClass) throws SchedulerException {
        resume(null, jobClass);
    }

    public void resume(String schedulerName, Class<? extends Job> jobClass) throws SchedulerException {
        getScheduler(schedulerName).resumeJob(new JobKey(jobClass.getSimpleName()));
    }

    public void resumeAll(Class<? extends Job> jobClass) throws SchedulerException {
        resumeAll(null, jobClass);
    }

    public void resumeAll(String schedulerName, Class<? extends Job> jobClass) throws SchedulerException {
        getScheduler(schedulerName).resumeAll();
    }

}
